<?php

namespace App\Http\Resources\Train\Screen;

use App\Models\Train\Stations\Repositories\StationRepository;
use App\Models\Train\Stations\TrainStation;
use App\Trip\Translate\Niutrans\Translation;
use Exception;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class ArrivingResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param Request $request
     * @return array
     * @throws Exception
     * @throws GuzzleException
     */
    public function toArray($request)
    {
        $res                      = [
            'train_code'     => $this->train_code ?? $this['train_code'],
            'station_code'   => $this->station_code ?? $this['station_code'],
            'arrival_time'   => $this->arrival_time ?? $this['arrival_time'],
            'exiting_port'   => (new Translation($this->exiting_port ?? $this['exiting_port']))->setTo(app()->getLocale())->translate()->getTranslation(),
            'arriving_state' => (new Translation($this->arriving_state ?? $this['arriving_state']))->setTo(app()->getLocale())->translate()->getTranslation(),
        ];
        $stationRepo              = new StationRepository(new TrainStation());
        $station                  = $stationRepo->findOneByCode($res['station_code']);
        $res ['start_station']    = $station->getStationName();
        $res ['start_station_uy'] = $station->getStationNameUy();
        return $res;
    }
}
